package mpbe.Componentes.TableColEdit;

/*
 * Este codigo es software libre y se encuentra licenciado 
 * bajo los términos de la Licencia Pública General de GNU versión 3 
 * según se encuentra publicada por la Free Software Foundation.
 * http://www.gnu.org/licenses/gpl.html
 * 
 * Autor: Martin Pablo Bellanca <martin.bellanca@gmail.com>
 * web: http://mpbe.blogspot.com/
 */

import org.eclipse.swt.custom.ControlEditor;
import org.eclipse.swt.custom.TableCursor;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;

public class WgBooleanCE extends WidgetsTCE {
	private ModifyCellListener modifyListener = null;
	
	public WgBooleanCE(int SWT, String[] lista, String txtdefault) {
		super(SWT, lista, txtdefault);
		
	}

	public WgBooleanCE(int SWT) {
		super(SWT);
		
	}

	public WgBooleanCE(int SWT, String[] lista) {
		super(SWT, lista);
		
	}

	public WgBooleanCE(int SWT, String txtdefault) {
		super(SWT, txtdefault);
		
	}

	@Override
	public Control getWidget() {
		
		return null;
	}

	@Override
	public int indexOfList(String TXT) {
		
		return -1;
	}

	@Override
	public void onCursoKeyChar(EditingTCE editando, TableCursor cursor,
			ControlEditor editor, Table table, char car) {
		for (int ii=0; ii<2; ii++){
			String txt = ""+car;		
			if ((TXTList[ii].startsWith(txt.toLowerCase())) || (TXTList[ii].startsWith(txt.toUpperCase()))) {			
				TableItem row = cursor.getRow();
				int column = cursor.getColumn();
				row.setText(column, TXTList[ii]);
				if (modifyListener!=null){
					if (ii==0) modifyListener.modifyCell(table.getSelectionIndex(), (Object) new Boolean(true));
					else modifyListener.modifyCell(table.getSelectionIndex(), (Object) new Boolean(false));
				}
			}
		}
	}

	@Override
	public void onCursorKeyF2(EditingTCE editando, TableCursor cursor,
			ControlEditor editor, Table table) {
		TableItem row = cursor.getRow();
		int column = cursor.getColumn();
		String txt = row.getText(column);
		if (txt.equals(TXTList[0])) {
			row.setText(column, TXTList[1]);
			if (modifyListener!=null) modifyListener.modifyCell(table.getSelectionIndex(), (Object) new Boolean(false));
		}
		else {
			row.setText(column, TXTList[0]);
			if (modifyListener!=null) modifyListener.modifyCell(table.getSelectionIndex(), (Object)new Boolean(true));
		}
		
	}

	@Override
	public void onCursorMouseDown(EditingTCE editando, TableCursor cursor,
			ControlEditor editor, Table table) {
		TableItem row = cursor.getRow();
		int column = cursor.getColumn();
		String txt = row.getText(column);
		if (txt.equals(TXTList[0])) {
			row.setText(column, TXTList[1]);
			if (modifyListener!=null) modifyListener.modifyCell(table.getSelectionIndex(), (Object) new Boolean(false));
		}
		else {
			row.setText(column, TXTList[0]);
			if (modifyListener!=null) modifyListener.modifyCell(table.getSelectionIndex(), (Object)new Boolean(true));
		}
	}

	@Override
	public void onTableMouseDown(EditingTCE editando) {
		

	}

	// funciones de listener
	public void addModifyCellListener(ModifyCellListener listener) {
		modifyListener = listener;
	}
	
	public void removeModifyCellListener() {
		modifyListener = null;
	}
}
